#include "fig_enemy_triangle.h"
#include "fig_enemybullet_master.h"
#include "fighter.h"
#include "fig_sfx_manager.h"
#include "fig_constants.h"
#include "fig_functions.h"

#include "num/rand.h"
#include "math/facing.h"
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging
#include "util/timer.h"		    // logging

FIGEnemyTriangle::FIGEnemyTriangle(FIGEnemyCraft::ENEMY_TYPES EnemyType) :
								FIGEnemyMiniBoss
										( 										
										EnemyType
										),								
								m_IsReadyToTranformback(false),
								m_IsGoBackLeft(false),
								m_GoBackSpeed(0)
{
	m_DamageForColliding = (float)FIGConstants::PLAYER_HP * 0.25f;
	m_ShootCounter.SetLimit(0.5f);
	m_MaxAttackCount = 15;
	m_IsHitTestCircle = true;
	m_ExplodeSize = 1.0f;
	m_MaxLife = m_Life = 10;
	m_ExplodSoundSfx = FIGSounds::EXPLOSION_SMALL_1;
}


bool	FIGEnemyTriangle::Tick_ComeIn(float dt,float BottomLimit)
{
	m_Rotation += ( 360 * dt * 0.5f);
	
	bool b = FIGEnemyMiniBoss::Tick_ComeIn(dt,BottomLimit);
	if(b)
	{
		if((int)m_Rotation % 360 < 5 || (int)m_Rotation % 360 > 355 )
		{
			m_Rotation = 0;
			return true;
		}
	}
	return false;

}

void	FIGEnemyTriangle::Tick_Attack(float dt)
{
	

	FIGEnemyMiniBoss::Tick_Attack(dt);

	m_Rotation += ( 360 * dt * 0.2f);
	//m_Rotation = 45;
	
	if(m_Rotation > 360.0f)
		m_Rotation = m_Rotation - 360.0f;
	if(m_IsReadyToTranformback)
	{
		if( (int)m_Rotation % 360 < 5)
		{
			m_Rotation = 0;
			m_MiniBossState = MINIBOSS_STATE_TranformBack;
		}
	}

	
}




void	FIGEnemyTriangle::OnExplode()
{
	FIGEnemyMiniBoss::OnExplode();
	FIGSfx* pSFX = FIGSFXManager::Instance().Spawn(	FIGSfx::CRAFT_PIECES_TRIANGLE_MAIN, 
											m_Pos,
											m_ExplodeSize,
											m_Rotation);
	if(pSFX)
	{
		AddSFX(pSFX);
		pSFX->SetIsRenderByOwner(true);
	
	}

	for(int i = 0; i < 3; i++)
	{
		//FIGEnemyMiniBoss::BodyPart* pBodypart = m_BodyParts[i];
		//FIGSFXManager::Instance().Spawn(FIGSfx::CRAFT_PIECES_TRIANGLE_FRAME 
		//								, m_Pos + pBodypart->m_Pos 
		//								, m_Rotation + pBodypart->m_Rotation);
	}

}

void	FIGEnemyTriangle::InternalShoot(int ShootIndex)
{
	float Rotation;
	FIGBonePart* pBonePart  = m_Bone.GetBonePartByID(ShootIndex+1);

	if(pBonePart)
	{
		Rotation = m_Rotation + pBonePart->GetRotation();
		pBonePart->Shoot(m_Rotation + 180);
	}
	
	math::Vec2 ShootPos = math::Vec2(0,1.0f);
	ShootPos.Rotate(-Rotation);
	ShootPos *= 0.035f;
	

	FIGEnemyBulletMaster::Instance().Shoot( 
											this,
											ShootPos , 
											FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_GLOW, 
											FIGBullet::SHOOT_STYLES_SINGLE, 
											Rotation + 180,
											0,
											false,
											NULL
											);




	if(m_MiniBossState == MINIBOSS_STATE_TranformBack)
	{
		// wait for the degree to become 0;
		m_MiniBossState = MINIBOSS_STATE_ATTACK;
		m_IsReadyToTranformback = true;
	}
}

void	FIGEnemyTriangle::OnShootStyle1()
{
	InternalShoot(0);	
}

void	FIGEnemyTriangle::OnShootStyle2()
{
	InternalShoot(1);	
}

void	FIGEnemyTriangle::OnShootStyle3()
{
	InternalShoot(2);	
}


void		FIGEnemyTriangle::Tick_GoBack(float dt)
{
	//backward
	math::Vec2 Dir;
	float Dist;

	
	if(m_IsGoBackLeft)
		Dir = math::Facing::VectorFromAngle(45);
	else
		Dir = math::Facing::VectorFromAngle(-45);
	
	Dir.Normalize();
	Dist = dt * 0.3f;
	m_Pos += ( Dir * Dist);

	//forward
	Dir = math::Facing::VectorFromAngle(180);
	Dir.Normalize();
	Dist = dt * m_GoBackSpeed;
	m_Pos += ( Dir * Dist);

	m_GoBackSpeed -= dt;

}

void		FIGEnemyTriangle::Tick_RotateBack(float dt)
{
	m_MiniBossState = MINIBOSS_STATE_GOBACK;
	m_GoBackSpeed = 0.6f;

	if(num::RAND32::Instance().RandInt(0,1) == 0)
		m_IsGoBackLeft = true;
	else
		m_IsGoBackLeft = false;
	
}

void		FIGEnemyTriangle::ReSpawn()
{
	FIGEnemyMiniBoss::ReSpawn();
	m_IsReadyToTranformback = false;
}
